Plex.Lib.CSS.load('UI.Widget.Fly_Option');


Plex.UI.Widget.Fly_Option = function(settings)
{
	var self = this;
	
	this.timeoutId = false;
		
	this.setting = {
		closeImage: '/img/close.png',
		closeHoverImage: '/img/close_hover.png',
		title: 'Untitled',
		description: '',
		timeout:0,
		options: []
	}
	
	this.hotKeys = {
		27 : function(){
			self.close();
		}
	}
	
	for(i in settings)
	this.setting[i]=settings[i];
	
	this.element = {}
	this.optionCount = 0;
	
	this.runHotKeys = function(e)
	{
		
		if(self.hotKeys[e.charCode])
		{
			self.hotKeys[e.charCode].apply(self);
		}
		else if(self.hotKeys[String.fromCharCode(e.charCode)])
		{
			self.hotKeys[String.fromCharCode(e.charCode)].apply(self);
		}
		else if(self.hotKeys[e.keyCode])
		{
			self.hotKeys[e.keyCode].apply(self);
		}
			
		e.stopPropagation();
	}
	
	this.init = function() 
	{
		self.element.sDiv = new Element('div',{
			'id':'overlay',
			'styles':{
				'opacity':0
			}
		})
		
		self.element.cDiv = new Element('div',{
			'class':'done',
			'id':'lightbox',
			'styles':{
				'opacity':0,
				'font-family':'"Lucida Grande", Tahoma, Arial, sans-serif',
				'font-size':'small',
			}
		}).injectInside(self.element.sDiv);
		
		self.element.lbDiv = new Element('div',{
			'id':'lbContent'
		}).injectInside(self.element.cDiv)
		
		self.element.titleH = new Element('h2').setText(self.setting.title).injectInside(self.element.lbDiv);
		self.element.descriptionH = new Element('h3').setText(self.setting.description).injectInside(self.element.lbDiv);
		self.element.optionList = new Element('ol',{'class':'decide'}).injectInside(self.element.lbDiv);
		self.element.nothingLink = new Element ('a',{'href':'#'}).injectInside(document.body);
		
		
		var newOption = function(option)
		{
			self.optionCount++;
			
			var newOp = new Element('li').adopt(
				newLink = new Element('a',{
					'href':option.href?option.href:'#',
					'styles':{
					},
					'class':self.optionCount%2?'a':'b',
					'events': {
						'click': function()
						{
							option.onclick ? option.onclick.apply(self) : void(0);
						}
					}
				})
			)
			
			if(option.hotKey)
			{
				if(typeof option.hotKey == 'object')
				{
					for(var i=0; i<option.hotKey.length; i++)
					self.hotKeys[option.hotKey[i]]=option.onclick;
				}
				else
				self.hotKeys[option.hotKey]=option.onclick;
			}
			
			self.hotKeys[self.optionCount+48]=option.onclick?option.onclick:function(){window.location=option.href};
				
			newLink.setHTML("<span>"+self.optionCount+".</span> "+option.text);
			
			return newOp;
		}
		
		for(var i=0; i<self.setting.options.length; i++)
		{
			var option = self.setting.options[i];
			self.element.optionList.adopt(new newOption(option))
			
		}
			
		self.fx = new Fx.Styles(self.element.sDiv, {
			duration: 250,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut
		});
			
		self.bfx = new Fx.Styles(self.element.cDiv, {
			duration: 500,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut
		});	
	},
	
	this.open = function()
	{		
		self.setting.lastOnKeyPress = document.body.onkeypress ? document.body.onkeypress : function(){};
		document.body.onkeypress = self.runHotKeys;
		
		
		self.element.sDiv.injectInside(document.body);	
		self.element.cDiv.injectInside(document.body);	
		
		self.element.nothingLink.focus();
		
		self.fx.start({
			'opacity':0.9
		});
		
		self.bfx.start({
			'opacity':1
		});
		
		if(self.setting.timeout>0)
		{
			self.timeoutId = setTimeout(function()
			{
				self.close();
			},self.setting.timeout*1000)
		}
	},
	
	this.close = function(onclose)
	{
		document.body.onkeypress = self.setting.lastOnKeyPress;
		document.onkeyPress = function(){}
		
		self.fx.start({
			'opacity':0
		});
		
		self.bfx.start({
			'opacity':0
		});
		
		if(self.timeoutId)
		{
			clearTimeout(self.timeoutId);
			self.timeoutId=false;
		}
	}
	
	this.init();
}